const express = require('express')
const cookieParse = require('cookie-parser')
const uuid = require('uuid')


const session = {}
let cardName = 'connect.sid'

const app = express()

app.use(cookieParse('ljp'))

app.get('/wash', function (req, res) {
    let cardNum = req.signedCookies[cardName] // 验证cookie
    if (cardNum && session[cardNum]) {
        session[cardNum].mny -= 10
        res.send('现在余额为: ' + session[cardNum].mny)
    } else { // 初次进入
        const cardId = uuid.v4() // 生成唯一值  原理 ipUrl+时间戳
        session[cardId] = { mny: 100 }
        res.cookie(cardName, cardId, { signed: true }) // 通过唯一id加盐生成cookie
        res.send('第一次来办卡')
    }
})


app.get('/read', function (req, res) {
    console.log(req)
    res.send(req.signedCookies.name)
})

app.get('/write', function (req, res) {
    res.cookie('name', 'ljp', { signed: true })
    res.send('写入成功')
})



app.listen(3000, function () {
    console.log('server start')
})
